class Result{
    public int sum = 0;
    public Object locker = new Object();
}
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Result result = new Result();
        Thread t = new Thread(()->{
            int sum = 0;
            for (int i = 0; i < 100; i++) {
                sum += i;
            }
            synchronized (result.locker){
                result.sum = sum;
                result.locker.notify();
                //完成加法操作,唤醒主线程
            }
        });
        t.start();

        synchronized (result.locker){
            while ( result.sum == 0){
                //当t1线程还没有结束时,主线程wait等待t1线程执行结束,在执行主线程打印结果
                result.locker.wait();
            }
            System.out.println(result.sum);
        }
    }
}
